<?php
namespace Amoysea\Controller;
use Think\Controller;
class GoodsController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
        $db_name=$g['db'];
        $this->product = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		$this->arrivalorder = M('arrival_order',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
		// $this->unit = M('unit',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }
	//上架
	public function edit(){
		$id=I('id');
		$unit_list=M('unit')->select();
		$detail=$this->arrivalorder->where(array('id'=>$id))->find();

        $this->assign('detail',$detail);
		$this->assign('unit_list',$unit_list);
		$this->display();
	}
	//编辑云仓数量并上架
	public function editD(){
		$id=I('id');

		$cloud_num=I('cloud_num');
		$cloud_depot_status=I('cloud_depot_status');
		$cloud_unit=I('cloud_unit');
        if($id=='' || !is_numeric($id)){
            alert(0,'操作有误');
        }
		$data['grounding_status']=1;

		$data['cloud_num']=$cloud_num;
		$data['cloud_depot_status']=$cloud_depot_status;
		$data['cloud_unit']=$cloud_unit;
		
		$res=$this->arrivalorder->where(array('id'=>$id))->save($data);
        if($res!==false){
            alert(1,'上架成功');
        }else{
            alert(0,'上架失败');
        }
    }
	//下架
	public function grounding($type,$id){
        if($id=='' || !is_numeric($id)){
            alert(0,'操作有误');
        }
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('arrival_order',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);

        $result=$this->p->where("id='".$id."'")->setField('grounding_status',$type);
        if($result!==false){
            alert(1,'操作成功');
        }else{
            alert(0,'操作失败');
        }
    }
	public function arrivalorder_list(){
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
		if(I("get.warehouse_code")!='') {
            $where['warehouse_code']= array('gt',I("get.warehouse_code"));
        }
        if(I("get.start_time")!='') {
            $where['add_time']= array('gt',I("get.start_time"));
        }
        if(I("get.end_time")!=''){
            $where['add_time'] = array('lt',I("get.end_time"));
        }
        if(I("get.start_time")!='' && I("get.end_time")!=''){
            $where['add_time']= array(array('gt',I("get.start_time")),array('lt',I("get.end_time")));
        }
        if(I("get.goods_name")!=''){
            $where['goods_name']=array('like',"%".I("get.goods_name")."%");
        }
		if(I("get.goods_no")!=''){
            $where['goods_no']=array('like',"%".I("get.goods_no")."%");
        }
        if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
		// dump(I());
        $f=C('DB_PREFIX_T');
        if(I("get.sort")!=''){
            $order=I("get.sort")." ".I("get.dec");
        }else{
            $order="id desc";
        }
        $list = $this->arrivalorder->where($where)->page($p.','.$page_count)->order($order)->select();
		// dump($this->arrivalorder);
        $count = $this->arrivalorder->where($where)->count();
		$m=M('Warehouse');

		foreach($list as $k=>$v){
			$unit_info=M('Unit')->where(array('unit_no'=>$v['unit1']))->find();
			$cloud_unit_info=M('Unit')->where(array('unit_no'=>$v['cloud_unit']))->find();
			$list[$k]['unit_name']=$unit_info['unit_name'];
			$list[$k]['cloud_unit_name']=$cloud_unit_info['unit_name'];
			$warehouse_info=$m->where(array('warehouse_code'=>$v['warehouse_code']))->find();
			$list[$k]['warehouse_name']=$warehouse_info['name'];
		}
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->display();
    }
    public function index(){
        if(!isset($_GET['p']) || $_GET['p']==''){
            $p=1;
        }else{
            $p=intval($_GET['p']);
        }
        if(I("get.start_time")!='') {
            $where['created_at']= array('gt',I("get.start_time"));
        }
        if(I("get.end_time")!=''){
            $where['created_at'] = array('lt',I("get.end_time"));
        }
        if(I("get.start_time")!='' && I("get.end_time")!=''){
            $where['created_at']= array(array('gt',I("get.start_time")),array('lt',I("get.end_time")));
        }
        if(I("get.name")!=''){
            $where['name']=array('like',"%".I("get.name")."%");
        }
        if(I("get.page_count")=='' || I("get.page_count")<=0){
            $page_count=15;
        }else{
            $page_count=intval(I("get.page_count"));
        }
        $f=C('DB_PREFIX_T');
        $where['is_delete']=0;
        if(I("get.sort")!=''){
            $order=$f."list_product.".I("get.sort")." ".I("get.dec");
        }else{
            $order=$f."list_product.id desc";
        }
        // $list = $this->product->field($f."product.*,".$f."list_product.*")
            // ->join($f."product on ".$f."product.id=".$f."list_product.product_id",'left')
            // ->where($where)->where($f."list_product.is_examined=1")->page($p.','.$page_count)->order($order)->select();
		$list = $this->product->field($f."product.*,".$f."list_product.*")
            ->join($f."product on ".$f."product.id=".$f."list_product.product_id",'left')
            ->where($where)->page($p.','.$page_count)->order($order)->select();
        $count      = $this->product->join($f."product on ".$f."product.id=".$f."list_product.product_id",'left')->where($where)->where($f."list_product.is_examined=1")->count();
        $this->assign('count',$count);
        $Page       = new \Think\Page($count,$page_count);
        $show       = $Page->show();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->display();
    }
    public function detailBack($id,$count){
        if($id=='' || $count==''){
            alert(0,'操作有误');
        }
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');////因为对方现在有list_product表的id字段---gooods_id  所以不需要那么复杂
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->p->where("id='".$id."'")->find();
        $data=array(
            'erp_code'=>$g['company_id'],
            'goods_id'=>$result['id'],
            'count'=>$count,
        );
        ////根据参数找到对应的仓库
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id",'left')
            ->where(C('DB_PREFIX')."warehouse.warehouse_code='".$result['warehouse_code']."'")
            ->getField('vars,url,warehouse_code');
        $url=$d['var_2']['url'];
        $warehouse_code=$d['var_2']['warehouse_code'];
        $data['warehouse_code']=$warehouse_code;
        $return =json_decode(push($url,$data),true);
        if($return['status']){
            $data['is_back']=2;
            $data['back_count']=$count;
            $data['erp_code']=$g['company_id'];
            $true=$this->p->where("id='".$result['id']."'")->save($data);
            if($true){
                alert(1,'回执成功');
            }else{
                alert(0,'回执成功，更新状态失败');
            }
        }else{
            alert(0,'回执失败');
        }
    }
    public function lastSure($id){
        if($id==''){
            alert(0,'操作有误');
        }
        $g=session('company');
        $db_name=$g['db'];
        $f=C('DB_PREFIX_T');////因为对方现在有list_product表的id字段---gooods_id  所以不需要那么复杂
        $this->p = M('list_product',$f, C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->p->where("id='".$id."'")->find();
        $data=array(
            'erp_code'=>$g['company_id'],
            'goods_id'=>$result['id'],
            'is_last'=>true,
        );
        ////根据参数找到对应的仓库
        $d=M("warehouse")->join("__INTERFACES__ on __INTERFACES__.warehouse_id=__WAREHOUSE__.id",'left')
            ->where(C('DB_PREFIX')."warehouse.warehouse_code='".$result['warehouse_code']."'")
            ->getField('vars,url,warehouse_code');
        $url=$d['var_3']['url'];
        $warehouse_code=$d['var_3']['warehouse_code'];
        $data['warehouse_code']=$warehouse_code;
        $return =push($url,$data);
        $result=json_decode($return,true);
        if($result['status']==1){
            $datas['act_count']=$this->p->where("id='".$id."'")->getField('back_count');
            $this->p->where("id='".$id."'")->save($datas);
            alert(1,'成功');
        }else{
            alert(0,'失败');
        }
    }
    public function shelves($type,$id){
        if($id=='' || !is_numeric($id) || ($type!=3 && $type!=4)){
            alert(0,'操作有误');
        }
        $g=session('company');
        $db_name=$g['db'];
        $this->p = M('list_product',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $list=$this->p->where("id='".$id."'")->find();
        if(!$list['id']){
            alert(0,'操作有误');
        }
        if($list['is_examined']==3){
            alert(0,'三级审核之后才能上架/下架');
        }
        if($list['act_count']===null){
            alert(0,'最终确认之后才能上架');
        }
        if($type==1 && $list['is_shelves']==1){
            alert(0,'请不要重复上架');
        }
        if($type==0 && $list['is_shelves']==0){
            alert(0,'请不要重复下架');
        }
        $result=$this->p->where("id='".$id."'")->setField('is_shelves',$type);
        if($result){
            alert(1,'操作成功');
        }else{
            alert(0,'操作失败');
        }
    }
    public function del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $where['id']=array('IN',$group_id);
        $result=$this->product->where($where)->setField('is_delete',1);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
}